package com.koron.css2.newProjectCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.bean.PjPubUpMaterial;
import com.koron.css2.newProjectCenter.bean.Warehouse;
import com.koron.css2.newProjectCenter.mapper.PjPubUpMaterialMapper;
import com.koron.css2.newProjectCenter.mapper.PubUpMaterialTypeMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.PingYin;
import org.swan.bean.MessageBean;

/**
 * @Author: 苏小龙
 * @CreateTime: 2024-12-04
 * @Description: 材料信息
 * @Version: 1.0
 */
public class PjPubUpMaterialSave implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            PjPubUpMaterial pubUpInvoice = JsonUtils.objectToPojo(req.getData(), PjPubUpMaterial.class);
            PjPubUpMaterialMapper pjPubUpInvoiceMapper = factory.getMapper(PjPubUpMaterialMapper.class);
            PubUpMaterialTypeMapper thisMapper = factory.getMapper(PubUpMaterialTypeMapper.class);
            // 1. id
            pubUpInvoice.setId(Tools.getObjectId());
            // 1. updateTime,updateAccount,updateName
            pubUpInvoice.setCreateInfo(userInfo);
            // 1.2 判断库存信息
            if (pubUpInvoice.getStoreList() == null || pubUpInvoice.getStoreList().isEmpty())
                return MessageBean.create(Constant.MESSAGE_INT_FAIL, "请输入仓库信息", null);
            if (pubUpInvoice.getStockQuantity() == null || pubUpInvoice.getStockQuantity() <= 0)
                return MessageBean.create(Constant.MESSAGE_INT_FAIL, "请输入库存数量", null);
            if (pubUpInvoice.getStockUpperLimit() == null || pubUpInvoice.getStockUpperLimit() <= 0)
                pubUpInvoice.setStockUpperLimit(0);
            if (pubUpInvoice.getStockLowerLimit() == null || pubUpInvoice.getStockLowerLimit() <= 0)
                pubUpInvoice.setStockLowerLimit(0);
            if (pubUpInvoice.getStockUpperLimit() < pubUpInvoice.getStockLowerLimit())
                return MessageBean.create(Constant.MESSAGE_INT_FAIL, "库存上限不能小于库存下限", null);
            if (pubUpInvoice.getStockUpperLimit() > 0 && pubUpInvoice.getStockQuantity() > pubUpInvoice.getStockUpperLimit())
                return MessageBean.create(Constant.MESSAGE_INT_FAIL, "库存数量不能大于库存上限", null);
            if (pubUpInvoice.getStockLowerLimit() > 0 && pubUpInvoice.getStockQuantity() < pubUpInvoice.getStockLowerLimit())
                return MessageBean.create(Constant.MESSAGE_INT_FAIL, "库存数量不能小于库存下限", null);

            // 将StoreList中的每个对象的quantity相加最后将总和打印
            pubUpInvoice.setStockQuantity(pubUpInvoice.getStoreList().stream()
                    .mapToInt(Warehouse::getQuantity)
                    .sum());

            // 2. 新增数据
            // 2.1 判断typeId 是否为空
            if (StringUtils.isEmpty(pubUpInvoice.getTypeId()))
                return MessageBean.create(Constant.MESSAGE_INT_FAIL, "请选择材料分类", null);
            // 2.2 获取最后一个code值，并生成新code
            int count = pjPubUpInvoiceMapper.getAllCountByTypeId(pubUpInvoice.getTypeId());
            String prefix = thisMapper.getCodeById(pubUpInvoice.getTypeId());
            pubUpInvoice.setCode(Tools.generateCode(prefix, count));
            // 2.3 自动生成材料名称拼音首字母简写
            pubUpInvoice.setMnemonicCode(PingYin.getInstance().getSelling(pubUpInvoice.getName(), true, ""));
            // 3. 保存仓库列表信息到库存表
            pubUpInvoice.getStoreList().forEach(warehouse -> {
                warehouse.setId(Tools.getObjectId());
                warehouse.setMaterialId(pubUpInvoice.getId());
                warehouse.setCreateInfo(userInfo);
                pjPubUpInvoiceMapper.insertIntoStore(warehouse);
            });
            if (pubUpInvoice.getSalesPrice().isEmpty())
                pubUpInvoice.setSalesPrice(null);
            if (pubUpInvoice.getCostPrice().isEmpty())
                pubUpInvoice.setCostPrice(null);
            pjPubUpInvoiceMapper.insert(pubUpInvoice);
            return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "保存成功", null);
        } catch (Exception e) {
            factory.close(false);
            logger.error("材料信息" + e.getMessage(), e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "保存失败", null);
        }
    }

    /**
     * 生成编码
     */
    public String generateCode(int code) {
        if (code >= 100) {
            return String.valueOf(code + 1);
        } else {
            String newCode = String.valueOf(code + 1);
            if (newCode.length() < 3) {
                return "0" + newCode;
            } else {
                return newCode;
            }
        }
    }
}
